

ALTER  proc sp_get_salespersons_list
@customerId int
as
begin

	declare @results Table(SalesPerson_ID int, SalesPerson_Name varchar(100), Flag varchar(10) )
	
	insert into @results(SalesPerson_ID, SalesPerson_Name )  select  SalesPerson_ID, SalesPerson_Name from Sales_Persons 
	where Deactivated <> 1 order by SalesPerson_Name
	
	declare salesPersonList CURSOR for select  csr.SalesPerson_ID from Customer_SalesPerson_Relationship csr, Customer_Relationship_Status crs where Customer_ID = @customerId and csr.RelationshipStatus_ID = crs.RelationshipStatus_ID and crs.Name = 'Active'
	declare @salesperson_id int

	open salesPersonList
		fetch next from salesPersonList into @salesperson_id
		while @@fetch_Status = 0
		begin
			update @results set Flag = 'checked' where SalesPerson_ID = @salesperson_id
			fetch next from salesPersonList into @salesperson_id
		end
	close salesPersonList
	deallocate salesPersonList
	
	select * from @results
end
